package org.hxzon.tapestry5.components;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.hxzon.project.SplitPageInfo;

@Import(library = { "SplitPageBar2.js" })
public class SplitPageBar2 {

    @Property
    @Parameter(required = true, principal = true)
    private SplitPageInfo pageInfo;

    @BeginRender
    void begin(MarkupWriter writer) {
        long currentPage = pageInfo.getCurrentPage();
        long pageCount = pageInfo.getPageCount();
        long value = 1;//openPage(this.value) ie have bug to send tag content to be button value
        writer.element("div", "class", "splitPageBar");
        writer.element("form", "method", "get");
        writer.element("button", "type", "button", "value", value, "disabled", currentPage <= 1 ? "disabled" : null, "onClick", "openPage(" + value + ")");
        writer.write("首页");
        writer.end();
        value = currentPage - 1;
        writer.element("button", "type", "button", "value", value, "disabled", currentPage <= 1 ? "disabled" : null, "onClick", ("openPage(" + value + ")"));
        writer.write("上一页");
        writer.end();
        value = currentPage + 1;
        writer.element("button", "type", "button", "value", value, "disabled", currentPage >= pageCount ? "disabled" : null, "onClick", "openPage(" + value + ")");
        writer.write("下一页");
        writer.end();
        value = pageCount;
        writer.element("button", "type", "button", "value", value, "disabled", currentPage >= pageCount ? "disabled" : null, "onClick", "openPage(" + value + ")");
        writer.write("尾页");
        writer.end();//button end
        writer.element("span");
        writer.write(currentPage + "/" + pageCount);
        writer.end();//span
        writer.element("select", "onChange", "openPage(this.value)");
        for (int i = 1; i < pageCount + 1; i++) {
            writer.element("option", "value", i, "selected", currentPage == i ? "selected" : null);
            writer.write("第" + i + "页");
            writer.end();
        }
        writer.end();//select
        writer.end();//form
        writer.end();//div
    }

}
